home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips.new / afm2tfm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-13  |  33.9 KB  |  1,290 lines

  1. /*
  2.  *   This program converts AFM files to TeX TFM files, and optionally
  3.  *   to TeX VPL files that retain all kerning and ligature information.
  4.  *   Both files make the characters not normally encoded by TeX available
  5.  *   by character codes greater than 127.
  6.  */
  7.  
  8. /*   (Modified by Don Knuth from Tom Rokicki's pre-VPL version.) */
  9.  
  10. #include <stdio.h>
  11. #ifdef SYSV
  12. #include <string.h>
  13. #else
  14. #ifdef VMS
  15. #include <string.h>
  16. #else
  17. #include <strings.h>
  18. #endif
  19. #endif
  20. #include <math.h>
  21.  
  22. #ifdef MSDOS
  23. #define WRITEBIN "wb"
  24. #else
  25. #define WRITEBIN "w"
  26. #endif
  27.  
  28. char *texencoding[] = {
  29.    "Gamma", "Delta", "Theta", "Lambda", "Xi", "Pi", "Sigma",
  30.    "Upsilon", "Phi", "Psi", "Omega", "arrowup", "arrowdown", "quotesingle",
  31.    "exclamdown", "questiondown", "dotlessi", "dotlessj", "grave", "acute",
  32.    "caron", "breve", "macron", "ring", "cedilla", "germandbls", "ae", "oe",
  33.    "oslash", "AE", "OE", "Oslash", "space", "exclam", "quotedbl", "numbersign",
  34.    "dollar", "percent", "ampersand", "quoteright", "parenleft", "parenright",
  35.    "asterisk", "plus", "comma", "hyphen", "period", "slash", "zero", "one",
  36.    "two", "three", "four", "five", "six", "seven", "eight", "nine", "colon",
  37.    "semicolon", "less", "equal", "greater", "question", "at", "A", "B", "C",
  38.    "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
  39.    "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft", "backslash",
  40.    "bracketright", "circumflex", "underscore", "quoteleft", "a", "b", "c", "d",
  41.    "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
  42.    "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright",
  43.    "tilde", "dieresis" } ;
  44. /*
  45.  *   The above layout corresponds to TeX Typewriter Type and is compatible
  46.  *   with TeX Text because the position of ligatures is immaterial.
  47.  */
  48.  
  49. /*
  50.  *   This is what we store Adobe data in.
  51.  */
  52. struct adobeinfo {
  53.    struct adobeinfo *next ;
  54.    int adobenum, texnum, width ;
  55.    char *adobename ;
  56.    int llx, lly, urx, ury ;
  57.    struct lig *ligs ;
  58.    struct kern *kerns ;
  59.    struct pcc *pccs ;
  60.    int wptr, hptr, dptr, iptr ;
  61. } *adobechars, *adobeptrs[256], *texptrs[256],
  62.   *uppercase[256], *lowercase[256] ;
  63. struct lig {
  64.    struct lig *next ;
  65.    char *succ, *sub ;
  66. } ;
  67. struct kern {
  68.    struct kern *next ;
  69.    char *succ ;
  70.    int delta ;
  71. } ;
  72. struct pcc {
  73.    struct pcc *next ;
  74.    char * partname ;
  75.    int xoffset, yoffset ;
  76. } ;
  77.  
  78. FILE *afmin, *vplout, *tfmout ;
  79. char inname[200], outname[200] ; /* names of input and output files */
  80. char buffer[255]; /* input buffer (modified while parsing) */
  81. char obuffer[255] ; /* unmodified copy of input buffer */
  82. char *param ; /* current position in input buffer */
  83. char *fontname = "Unknown" ;
  84. char *codingscheme = "Unspecified" ;
  85. float italicangle = 0.0 ;
  86. char fixedpitch ;
  87. char makevpl ;
  88. int xheight = 400 ;
  89. int fontspace ;
  90. int bc, ec ;
  91. long cksum ;
  92. float efactor = 1.0, slant = 0.0 ;
  93. double newslant ;
  94. char titlebuf[100] ;
  95.  
  96. void
  97. error(s)
  98. register char *s ;
  99. {
  100.    extern void exit() ;
  101.  
  102.    (void)fprintf(stderr, "%s\n", s) ;
  103.    if (obuffer[0]) {
  104.       (void)fprintf(stderr, "%s\n", obuffer) ;
  105.       while (param > buffer) {
  106.          (void)fprintf(stderr, " ") ;
  107.          param-- ;
  108.       }
  109.       (void)fprintf(stderr, "^\n") ;
  110.    }
  111.    if (*s == '!')
  112.       exit(1) ;
  113. }
  114.  
  115. int
  116. transform(x,y)
  117.    register int x,y ;
  118. {
  119.    register double acc ;
  120.    acc = efactor * x + slant *y ;
  121.    return (int)(acc>=0? acc+0.5 : acc-0.5 ) ;
  122. }
  123.  
  124. int
  125. getline() {
  126.    register char *p ;
  127.    register int c ;
  128.  
  129.    param = buffer ;
  130.    for (p=buffer; (c=getc(afmin)) != EOF && c != 10;)
  131.       *p++ = c ;
  132.    *p = 0 ;
  133.    (void)strcpy(obuffer, buffer) ;
  134.    if (p == buffer && c == EOF)
  135.       return(0) ;
  136.    else
  137.       return(1) ;
  138. }
  139.  
  140. char *interesting[] = { "FontName", "ItalicAngle", "IsFixedPitch",
  141.    "XHeight", "C", "KPX", "CC", "EncodingScheme", NULL} ; 
  142. #define FontName (0)
  143. #define ItalicAngle (1)
  144. #define IsFixedPitch (2)
  145. #define XHeight (3)
  146. #define C (4)
  147. #define KPX (5)
  148. #define CC (6)
  149. #define EncodingScheme (7)
  150. #define NONE (-1)
  151. int
  152. interest(s)
  153. char *s ;
  154. {
  155.    register char **p ;
  156.    register int n ;
  157.  
  158.    for (p=interesting, n=0; *p; p++, n++)
  159.       if (strcmp(s, *p)==0)
  160.          return(n) ;
  161.    return(NONE) ;
  162. }
  163.  
  164. char *
  165. mymalloc(len)
  166. int len ;
  167. {
  168.    register char *p ;
  169.    extern char *malloc() ;
  170.  
  171.    p = malloc((unsigned)len) ;
  172.    if (p==NULL)
  173.       error("! out of memory") ;
  174.    return(p) ;
  175. }
  176.  
  177. char *
  178. paramnewstring() {
  179.    register char *p, *q ;
  180.  
  181.    p = param ;
  182.    while (*p > ' ')
  183.       p++ ;
  184.    q = mymalloc((int)(p-param+1)) ;
  185.    if (*p != 0)
  186.       *p++ = 0 ;
  187.    (void)strcpy(q, param) ;
  188.    while (*p && *p <= ' ')
  189.       p++ ;
  190.    param = p ;
  191.    return(q) ;
  192. }
  193.  
  194. char *
  195. paramstring() {
  196.    register char *p, *q ;
  197.  
  198.    p = param ;
  199.    while (*p > ' ')
  200.       p++ ;
  201.    q = param ;
  202.    if (*p != 0)
  203.       *p++ = 0 ;
  204.    while (*p && *p <= ' ')
  205.       p++ ;
  206.    param = p ;
  207.    return(q) ;
  208. }
  209.  
  210. int
  211. paramnum() {
  212.    register char *p ;
  213.    int i ;
  214.  
  215.    p = paramstring() ;
  216.    if (sscanf(p, "%d", &i) != 1)
  217.       error("! integer expected") ;
  218.    return(i) ;
  219. }
  220.  
  221. float
  222. paramfloat() {
  223.    register char *p ;
  224.    float i ;
  225.  
  226.    p = paramstring() ;
  227.    if (sscanf(p, "%f", &i) != 1)
  228.       error("! number expected") ;
  229.    return(i) ;
  230. }
  231.  
  232. struct adobeinfo *
  233. newchar() {
  234.    register struct adobeinfo *ai ;
  235.  
  236.    ai = (struct adobeinfo *)mymalloc(sizeof(struct adobeinfo)) ;
  237.    ai->adobenum = -1 ;
  238.    ai->texnum = -1 ;
  239.    ai->width = -1 ;
  240.    ai->adobename = NULL ;
  241.    ai->llx = -1 ;
  242.    ai->lly = -1 ;
  243.    ai->urx = -1 ;
  244.    ai->ury = -1 ;
  245.    ai->ligs = NULL ;
  246.    ai->kerns = NULL ;
  247.    ai->pccs = NULL ;
  248.    ai->next = adobechars ;
  249.    adobechars = ai ;
  250.    return(ai) ;
  251. }
  252.  
  253. struct kern *
  254. newkern() {
  255.    register struct kern *nk ;
  256.  
  257.    nk = (struct kern *)mymalloc(sizeof(struct kern)) ;
  258.    nk->next = NULL ;
  259.    nk->succ = NULL ;
  260.    nk->delta = 0 ;
  261.    return(nk) ;
  262. }
  263.  
  264. struct pcc *
  265. newpcc() {
  266.    register struct pcc *np ;
  267.  
  268.    np = (struct pcc *)mymalloc(sizeof(struct pcc)) ;
  269.    np->next = NULL ;
  270.    np->partname = NULL ;
  271.    np->xoffset = 0 ;
  272.    np->yoffset = 0 ;
  273.    return(np) ;
  274. }
  275.  
  276. struct lig *
  277. newlig() {
  278.    register struct lig *nl ;
  279.  
  280.    nl = (struct lig *)mymalloc(sizeof(struct lig)) ;
  281.    nl->next = NULL ;
  282.    nl->succ = NULL ;
  283.    nl->sub = NULL ;
  284.    return(nl) ;
  285. }
  286.  
  287. void
  288. expect(s)
  289. char *s ;
  290. {
  291.    if (strcmp(paramstring(), s) != 0) {
  292.       (void)fprintf(stderr, "%s expected: ", s) ;
  293.       error("! syntax error") ;
  294.    }
  295. }
  296.  
  297. void
  298. handlechar() { /* an input line beginning with C */
  299.    register struct adobeinfo *ai ;
  300.    register struct lig *nl ;
  301.  
  302.    ai = newchar() ;
  303.    ai->adobenum = paramnum() ;
  304.    expect(";") ;
  305.    expect("WX") ;
  306.    ai->width = transform(paramnum(),0) ;
  307.    if (ai->adobenum >= 0 && ai->adobenum < 256) {
  308.       adobeptrs[ai->adobenum] = ai ;
  309.       cksum = (cksum<<1) ^ ai->width ;
  310.    }
  311.    expect(";") ;
  312.    expect("N") ;
  313.    ai->adobename = paramnewstring() ;
  314.    expect(";") ;
  315.    expect("B") ;
  316.    ai->llx = paramnum() ;
  317.    ai->lly = paramnum() ;
  318.    ai->llx = transform(ai->llx, ai->lly) ;
  319.    ai->urx = paramnum() ;
  320.    ai->ury = paramnum() ;
  321.    ai->urx = transform(ai->urx, ai->ury) ;
  322. /* We need to avoid negative heights or depths. They break accents in
  323.    math mode, among other things.  */
  324.    if (ai->lly > 0)
  325.       ai->lly = 0 ;
  326.    if (ai->ury < 0)
  327.       ai->ury = 0 ;
  328.    expect(";") ;
  329. /* Now look for ligatures (which aren't present in fixedpitch fonts) */
  330.    while (*param == 'L') {
  331.       expect("L") ;
  332.       nl = newlig() ;
  333.       nl->succ = paramnewstring() ;
  334.       nl->sub = paramnewstring() ;
  335.       nl->next = ai->ligs ;
  336.       ai->ligs = nl ;
  337.       expect(";") ;
  338.    }
  339.    if (strcmp(ai->adobename, "space")==0) {
  340.       fontspace = ai->width ;
  341.       nl = newlig() ;        /* space will act as zero-width Polish crossbar */
  342.       nl->succ = "l" ;       /* when used by plain TeX's \l or \L macros */
  343.       nl->sub = "lslash" ;
  344.       nl->next = ai->ligs ;
  345.       ai->ligs = nl ;
  346.       nl = newlig() ;
  347.       nl->succ = "L" ;
  348.       nl->sub = "Lslash" ;
  349.       nl->next = ai->ligs ;
  350.       ai->ligs = nl ;
  351.    } else if (strcmp(ai->adobename, "question")==0) {
  352.       nl = newlig() ;
  353.       nl->succ = "quoteleft" ;
  354.       nl->sub = "questiondown" ;
  355.       nl->next = ai->ligs ;
  356.       ai->ligs = nl ;
  357.    } else if (strcmp(ai->adobename, "exclam")==0) {
  358.       nl = newlig() ;
  359.       nl->succ = "quoteleft" ;
  360.       nl->sub = "exclamdown" ;
  361.       nl->next = ai->ligs ;
  362.       ai->ligs = nl ;
  363.    } else if (! fixedpitch) {
  364.       if (strcmp(ai->adobename, "hyphen")==0) {
  365.          nl = newlig() ;
  366.          nl->succ = "hyphen" ;
  367.          nl->sub = "endash" ;
  368.          nl->next = ai->ligs ;
  369.          ai->ligs = nl ;
  370.       } else if (strcmp(ai->adobename, "endash")==0) {
  371.          nl = newlig() ;
  372.          nl->succ = "hyphen" ;
  373.          nl->sub = "emdash" ;
  374.          nl->next = ai->ligs ;
  375.          ai->ligs = nl ;
  376.       } else if (strcmp(ai->adobename, "quoteleft")==0) {
  377.          nl = newlig() ;
  378.          nl->succ = "quoteleft" ;
  379.          nl->sub = "quotedblleft" ;
  380.          nl->next = ai->ligs ;
  381.          ai->ligs = nl ;
  382.       } else if (strcmp(ai->adobename, "quoteright")==0) {
  383.          nl = newlig() ;
  384.          nl->succ = "quoteright" ;
  385.          nl->sub = "quotedblright" ;
  386.          nl->next = ai->ligs ;
  387.          ai->ligs = nl ;
  388.       }
  389.    }
  390. }
  391.  
  392. struct adobeinfo *
  393. findadobe(p)
  394. char *p ;
  395. {
  396.    register struct adobeinfo *ai ;
  397.  
  398.    for (ai=adobechars; ai; ai = ai->next)
  399.       if (strcmp(p, ai->adobename)==0)
  400.          return(ai) ;
  401.    return(NULL) ;
  402. }
  403.  
  404. /* We ignore kerns before and after space characters, because (1) TeX
  405.  * is using the space only for Polish ligatures, and (2) TeX's
  406.  * boundarychar mechanisms are not oriented to kerns (they apply
  407.  * to both spaces and punctuation) so we don't want to use them. */
  408. void
  409. handlekern() { /* an input line beginning with KPX */
  410.    register struct adobeinfo *ai ;
  411.    register char *p ;
  412.    register struct kern *nk ;
  413.  
  414.    p = paramstring() ;
  415.    if (strcmp(p,"space")==0) return ;
  416.    ai = findadobe(p) ;
  417.    if (ai == NULL)
  418.       error("! kern char not found") ;
  419.    if (ai->adobenum<'0' || ai->adobenum>'9') {
  420. /* Ignore kerns after digits because they would mess up our nice tables */
  421.       nk = newkern() ;
  422.       nk->succ = paramnewstring() ;
  423.       if (strcmp(nk->succ,"space")==0) return ;
  424.       nk->delta = transform(paramnum(),0) ;
  425.       nk->next = ai->kerns ;
  426.       ai->kerns = nk ;
  427.     }
  428. }
  429.  
  430. void
  431. handleconstruct() { /* an input line beginning with CC */
  432.    register struct adobeinfo *ai ;
  433.    register char *p ;
  434.    register struct pcc *np ;
  435.    register int n ;
  436.    struct pcc *npp = NULL;
  437.  
  438.    p = paramstring() ;
  439.    ai = findadobe(p) ;
  440.    if (ai == NULL)
  441.       error("! composite character name not found") ;
  442.    n = paramnum() ;
  443.    expect(";") ;
  444.    while (n--) {
  445.       if (strcmp(paramstring(),"PCC") != 0) return ;
  446.         /* maybe I should expect("PCC") instead, but I'm playing it safe */
  447.       np = newpcc() ;
  448.       np->partname = paramnewstring() ;
  449.       if (findadobe(np->partname)==NULL) return ;
  450.       np->xoffset = paramnum() ;
  451.       np->yoffset = paramnum() ;
  452.       np->xoffset = transform(np->xoffset, np->yoffset) ;
  453.       if (npp) npp->next = np ;
  454.       else ai->pccs = np ;
  455.       npp = np ;
  456.       expect(";") ;
  457.    }
  458. }
  459.  
  460. void
  461. makeaccentligs() {
  462.    register struct adobeinfo *ai, *aci ;
  463.    register char *p ;
  464.    register struct lig *nl ;
  465.    for (ai=adobechars; ai; ai=ai->next) {
  466.       p = ai->adobename ;
  467.       if (strlen(p)>2)
  468.          if ((aci=findadobe(p+1)) && (aci->adobenum > 127)) {
  469.             nl = newlig() ;
  470.             nl->succ = mymalloc(2) ;
  471.             *(nl->succ + 1) = 0 ;
  472.             *(nl->succ) = *p ;
  473.             nl->sub = ai->adobename ;
  474.             nl->next = aci->ligs ;
  475.             aci->ligs = nl ;
  476.          }
  477.    }
  478. }
  479.  
  480. void
  481. readadobe() {
  482.    while (getline()) {
  483.       switch(interest(paramstring())) {
  484. case FontName:
  485.          fontname = paramnewstring() ;
  486.          break ;
  487. case EncodingScheme:
  488.          codingscheme = paramnewstring() ;
  489.          break ;
  490. case ItalicAngle:
  491.          italicangle = paramfloat() ;
  492.          break ;
  493. case IsFixedPitch:
  494.          if (*param == 't' || *param == 'T')
  495.             fixedpitch = 1 ;
  496.          else
  497.             fixedpitch = 0 ;
  498.          break ;
  499. case XHeight:
  500.          xheight = paramnum() ;
  501.          break ;
  502. case C:
  503.          handlechar() ;
  504.          break ;
  505. case KPX:
  506.          handlekern() ;
  507.          break ;
  508. case CC:
  509.          handleconstruct() ;
  510.          break ;
  511. default:
  512.          break ;
  513.       }
  514.    }
  515.    makeaccentligs() ;
  516. }
  517.  
  518. void
  519. assignchars() {
  520.    register char **p ;
  521.    register int i ;
  522.    register struct adobeinfo *ai ;
  523.    int nextfree = 128 ;
  524.  
  525. /*
  526.  *   First, we assign all those that match perfectly.
  527.  */
  528.    for (i=0, p=texencoding; i<128; i++, p++)
  529.       if ((ai=findadobe(*p)) && ai->adobenum >= 0) {
  530.          ai->texnum = i ;
  531.          texptrs[i] = ai ;
  532.       }
  533. /*
  534.  *   Next, we assign all the others whose codes are above 127, retaining
  535.  *   the adobe positions. */
  536.    for (ai=adobechars; ai; ai=ai->next)
  537.       if (ai->adobenum > 127 && ai->texnum<0) {
  538.          ai->texnum = ai->adobenum ;
  539.          texptrs[ai->adobenum] = ai ;
  540.       }
  541. /*   Finally, we map all remaining characters into free locations beginning
  542.  *   with 128, if we know how to construct those characters. */
  543.    for (ai=adobechars; ai; ai=ai->next)
  544.       if (ai->texnum<0 && (ai->adobenum>=0 || ai->pccs !=NULL)) {
  545.          while (texptrs[nextfree]) {
  546.             nextfree=(nextfree+1)&255 ;
  547.             if (nextfree==128) return ; /* all slots full */
  548.          }
  549.          ai->texnum = nextfree ;
  550.          texptrs[nextfree] = ai ;
  551.       }
  552. }
  553.  
  554. void
  555. upmap() { /* Compute uppercase mapping, when making a small caps font */
  556.    register struct adobeinfo *ai, *Ai ;
  557.    register char *p, *q ;
  558.    register struct pcc *np, *nq ;
  559.    char lwr[50] ;
  560.  
  561.    for (Ai=adobechars; Ai; Ai=Ai->next) {
  562.       p = Ai->adobename ;
  563.       if (*p>='A' && *p<='Z') {
  564.          q = lwr ;
  565.          for (; *p; p++)
  566.             *q++ = ((*p>='A' && *p<='Z') ? *p+32 : *p) ;
  567.          *q = 0;
  568.          if (ai=findadobe(lwr)) {
  569.             if (ai->texnum>=0) uppercase[ai->texnum] = Ai ;
  570.             if (Ai->texnum>=0) lowercase[Ai->texnum] = ai ;
  571.          }
  572.       }
  573.    }
  574. /* Note that, contrary to the normal true/false conventions,
  575.  * uppercase[i] is NULL and lowercase[i] is non-NULL when i is the
  576.  * ASCII code of an uppercase letter; and vice versa for lowercase letters */
  577.  
  578.    if (ai=findadobe("germandbls"))
  579.       if (Ai=findadobe("S")) { /* we also construct SS */
  580.          uppercase[ai->texnum] = ai ;
  581.          ai->width = Ai->width << 1 ;
  582.          ai->llx = Ai->llx ;
  583.          ai->lly = Ai->lly ;
  584.          ai->urx = Ai->width + Ai->urx ;
  585.          ai->ury = Ai->ury ;
  586.          ai->kerns = Ai->kerns ;
  587.          np = newpcc() ;
  588.          np->partname = "S" ;
  589.          nq = newpcc() ;
  590.          nq->partname = "S" ;
  591.          nq->xoffset = Ai->width ;
  592.          np->next = nq ;   
  593.          ai->pccs = np ;
  594.       }
  595.    if (ai=findadobe("dotlessi"))
  596.       uppercase[ai->texnum] = findadobe("I") ;
  597.    if (ai=findadobe("dotlessj"))
  598.       uppercase[ai->texnum] = findadobe("J") ;
  599. }
  600. /* The logic above seems to work well enough, but it leaves useless characters
  601.  * like `fi' and `fl' in the font if they were present initially,
  602.  * and it omits characters like `dotlessj' if they are absent initially */
  603.  
  604. /* Now we turn to computing the TFM file */
  605.  
  606. int lf, lh, nw, nh, nd, ni, nl, nk, ne, np ;
  607.  
  608. void
  609. write16(what)
  610. register short what ;
  611. {
  612.    (void)fputc(what >> 8, tfmout) ;
  613.    (void)fputc(what & 255, tfmout) ;
  614. }
  615.  
  616. void
  617. writearr(p, n)
  618. register long *p ;
  619. register int n ;
  620. {
  621.    while (n) {
  622.       write16((short)(*p >> 16)) ;
  623.       write16((short)(*p & 65535)) ;
  624.       p++ ;
  625.       n-- ;
  626.    }
  627. }
  628.  
  629. void
  630. makebcpl(p, s, n)
  631. register long *p ;
  632. register char *s ;
  633. register int n ;
  634. {
  635.    register long t ;
  636.    register long sc ;
  637.  
  638.    if (strlen(s) < n)
  639.       n = strlen(s) ;
  640.    t = ((long)n) << 24 ;
  641.    sc = 16 ;
  642.    while (n > 0) {
  643.       t |= ((long)(*(unsigned char *)s++)) << sc ;
  644.       sc -= 8 ;
  645.       if (sc < 0) {
  646.          *p++ = t ;
  647.          t = 0 ;
  648.          sc = 24 ;
  649.       }
  650.       n-- ;
  651.    }
  652.    *p++ = t ;
  653. }
  654.  
  655. int source[257] ;
  656. int unsort[257] ;
  657.  
  658. /*
  659.  *   Next we need a routine to reduce the number of distinct dimensions
  660.  *   in a TFM file. Given an array what[0]...what[oldn-1], we want to
  661.  *   group its elements into newn clusters, in such a way that the maximum
  662.  *   difference between elements of a cluster is as small as possible.
  663.  *   Furthermore, what[0]=0, and this value must remain in a cluster by
  664.  *   itself. Data such as `0 4 6 7 9' with newn=3 shows that an iterative
  665.  *   scheme in which 6 is first clustered with 7 will not work. So we
  666.  *   borrow a neat algorithm from METAFONT to find the true optimum.
  667.  *   Memory location what[oldn] is set to 0x7fffffffL for convenience.
  668.  */
  669. long nextd ; /* smallest value that will give a different mincover */
  670. int
  671. mincover(what,d) /* tells how many clusters result, given max difference d */
  672. register long d ;
  673. long *what ;
  674. {
  675.    register int m ;
  676.    register long l ;
  677.    register long *p ;
  678.  
  679.    nextd = 0x7fffffffL ;
  680.    p = what+1 ;
  681.    m = 1 ;
  682.    while (*p<0x7fffffffL) {
  683.       m++ ;
  684.       l = *p ;
  685.       while (*++p <= l+d) ;
  686.       if (*p-l < nextd) nextd = *p-l ;
  687.    }
  688.    return (m) ;
  689. }
  690.  
  691. void
  692. remap(what, oldn, newn)
  693. long *what ;
  694. int oldn, newn ;
  695. {
  696.    register int i, j ;
  697.    register long d, l ;
  698.  
  699.    what[oldn] = 0x7fffffffL ;
  700.    for (i=oldn-1; i>0; i--) {
  701.       d = what[i] ;
  702.       for (j=i; what[j+1]<d; j++) {
  703.          what[j] = what[j+1] ;
  704.          source[j] = source[j+1] ;
  705.       }
  706.       what[j] = d ;
  707.       source[j] = i ;
  708.    } /* Tom, don't let me ever catch you using bubblesort again! -- Don */
  709.  
  710.    i = mincover(what, 0L) ;
  711.    d = nextd ;
  712.    while (mincover(what,d+d)>newn) d += d ;
  713.    while (mincover(what,d)>newn) d = nextd ;
  714.  
  715.    i = 1 ;
  716.    j = 0 ;
  717.    while (i<oldn) {
  718.       j++ ;
  719.       l = what[i] ;
  720.       unsort[source[i]] = j ;
  721.       while (what[++i] <= l+d) {
  722.          unsort[source[i]] = j ;
  723.          if (i-j == oldn-newn) d = 0 ;
  724.       }
  725.       what[j] = (l+what[i-1])/2 ;
  726.    }
  727. }
  728.  
  729. /*
  730.  *   The next routine simply scales something.
  731.  *   Input is in 1000ths of an em.  Output is in FIXFACTORths of 1000.
  732.  */
  733. #define FIXFACTOR (0x100000L) /* 2^{20}, the unit fixnum */
  734. long
  735. scale(what)
  736. long what ;
  737. {
  738.    return(((what / 1000) << 20) +
  739.           (((what % 1000) << 20) + 500) / 1000) ;
  740. }
  741.  
  742. long *header, *charinfo, *width, *height, *depth, *ligkern, *kern, *tparam,
  743.      *italic ;
  744. long tfmdata[10000] ;
  745.  
  746. void
  747. buildtfm() {
  748.    register int i, j ;
  749.    register struct adobeinfo *ai ;
  750.  
  751.    header = tfmdata ;
  752.    header[0] = cksum ;
  753.    header[1] = 0xa00000 ; /* 10pt design size */
  754.    makebcpl(header+2, codingscheme, 39) ;
  755.    makebcpl(header+12, fontname, 19) ;
  756.    lh = 17 ;
  757.    charinfo = header + lh ;
  758.  
  759.    for (i=0; i<256 && adobeptrs[i]==NULL; i++) ;
  760.    bc = i ;
  761.    for (i=255; i>=0 && adobeptrs[i]==NULL; i--) ;
  762.    ec = i;
  763.    if (ec < bc)
  764.       error("! no Adobe characters") ;
  765.  
  766.    width = charinfo + (ec - bc + 1) ;
  767.    width[0] = 0 ;
  768.    nw++ ;
  769.    for (i=bc; i<=ec; i++)
  770.       if (ai=adobeptrs[i]) {
  771.          width[nw]=ai->width ;
  772.          for (j=1; width[j]!=ai->width; j++) ;
  773.          ai->wptr = j ;
  774.          if (j==nw)
  775.             nw++ ;
  776.       }
  777.    if (nw>256)
  778.       error("! 256 chars with different widths") ;
  779.    depth = width + nw ;
  780.    depth[0] = 0 ;
  781.    nd = 1 ;
  782.    for (i=bc; i<=ec; i++)
  783.       if (ai=adobeptrs[i]) {
  784.          depth[nd] = -ai->lly ;
  785.          for (j=0; depth[j]!=-ai->lly; j++) ;
  786.          ai->dptr = j ;
  787.          if (j==nd)
  788.             nd++ ;
  789.       }
  790.    if (nd > 16) {
  791.       remap(depth, nd, 16) ;
  792.       nd = 16 ;
  793.       for (i=bc; i<=ec; i++)
  794.          if (ai=adobeptrs[i])
  795.             ai->dptr = unsort[ai->dptr] ;
  796.    }
  797.    height = depth + nd ;
  798.    height[0] = 0 ;
  799.    nh = 1 ;
  800.    for (i=bc; i<=ec; i++)
  801.       if (ai=adobeptrs[i]) {
  802.          height[nh]=ai->ury ;
  803.          for (j=0; height[j]!=ai->ury; j++) ;
  804.          ai->hptr = j ;
  805.          if (j==nh)
  806.             nh++ ;
  807.       }
  808.    if (nh > 16) {
  809.       remap(height, nh, 16) ;
  810.       nh = 16 ;
  811.       for (i=bc; i<=ec; i++)
  812.          if (ai=adobeptrs[i])
  813.             ai->hptr = unsort[ai->hptr] ;
  814.    }
  815.    italic  = height + nh ;
  816.    italic[0] = 0 ;
  817.    ni = 1 ;
  818.    for (i=bc; i<=ec; i++)
  819.       if (ai=adobeptrs[i]) {
  820.          italic[ni] = ai->urx - ai->width ;
  821.          if (italic[ni]<0)
  822.             italic[ni] = 0 ;
  823.          for (j=0; italic[j]!=italic[ni]; j++) ;
  824.          ai->iptr = j ;
  825.          if (j==ni)
  826.             ni++ ;
  827.       }
  828.    if (ni > 64) {
  829.       remap(italic, ni, 64) ;
  830.       ni = 64 ;
  831.       for (i=bc; i<=ec; i++)
  832.          if (ai=adobeptrs[i])
  833.             ai->iptr = unsort[ai->iptr] ;
  834.    }
  835.  
  836.    for (i=bc; i<=ec; i++)
  837.       if (ai=adobeptrs[i])
  838.          charinfo[i-bc] = ((long)(ai->wptr)<<24) +
  839.                            ((long)(ai->hptr)<<20) +
  840.                             ((long)(ai->dptr)<<16) +
  841.                              ((long)(ai->iptr)<<10) ;
  842.  
  843.    ligkern = italic + ni ;
  844.    nl = 0 ; /* ligatures and kerns omitted from raw Adobe font */
  845.    kern = ligkern + nl ;
  846.    nk = 0 ;
  847.  
  848.    newslant = (double)slant - efactor * tan(italicangle*(3.1415926535/180.0)) ;
  849.    tparam = kern + nk ;
  850.    tparam[0] = (long)(FIXFACTOR * newslant + 0.5) ;
  851.    tparam[1] = scale((long)(fontspace*efactor+0.5)) ;
  852.    tparam[2] = (fixedpitch ? 0 : scale((long)(300*efactor+0.5))) ;
  853.    tparam[3] = (fixedpitch ? 0 : scale((long)(100*efactor+0.5))) ;
  854.    tparam[4] = scale((long)xheight) ;
  855.    tparam[5] = scale((long)(1000*efactor+0.5)) ;
  856.    np = 6 ;
  857. }
  858.  
  859. void
  860. writesarr(what, len)
  861. long *what ;
  862. int len ;
  863. {
  864.    register long *p ;
  865.    int i ;
  866.  
  867.    p = what ;
  868.    i = len ;
  869.    while (i) {
  870.       *p = scale(*p) ;
  871.       p++ ;
  872.       i-- ;
  873.    }
  874.    writearr(what, len) ;
  875. }
  876.  
  877. void
  878. writetfm() {
  879.    lf = 6 + lh + (ec - bc + 1) + nw + nh + nd + ni + nl + nk + ne + np ;
  880.    write16(lf) ;
  881.    write16(lh) ;
  882.    write16(bc) ;
  883.    write16(ec) ;
  884.    write16(nw) ;
  885.    write16(nh) ;
  886.    write16(nd) ;
  887.    write16(ni) ;
  888.    write16(nl) ;
  889.    write16(nk) ;
  890.    write16(ne) ;
  891.    write16(np) ;
  892.    writearr(header, lh) ;
  893.    writearr(charinfo, ec-bc+1) ;
  894.    writesarr(width, nw) ;
  895.    writesarr(height, nh) ;
  896.    writesarr(depth, nd) ;
  897.    writesarr(italic, ni) ;
  898.    writearr(ligkern, nl) ;
  899.    writesarr(kern, nk) ;
  900.    writearr(tparam, np) ;
  901. }
  902.  
  903. /* OK, the TFM file is done! Now for our next trick, the VPL file. */
  904.  
  905. /* For TeX we want to compute a character height that works properly
  906.  * with accents. The following list of accents doesn't need to be complete. */
  907. char *accents[] = { "acute", "tilde", "caron", "dieresis", NULL} ;
  908. int
  909. texheight(ai)
  910. register struct adobeinfo *ai ;
  911. {
  912.    register char **p;
  913.    register struct adobeinfo *aci, *acci ;
  914.    if (*(ai->adobename + 1)) return (ai->ury) ; /* that was the simple case */
  915.    for (p=accents; *p; p++)  /* otherwise we look for accented letters */
  916.       if (aci=findadobe(*p)) {
  917.          strcpy(buffer,ai->adobename) ;
  918.          strcat(buffer,*p) ;
  919.          if (acci=findadobe(buffer)) return (acci->ury - aci->ury + xheight) ;
  920.       }
  921.    return (ai->ury) ;
  922. }
  923.  
  924. /* modified tgr to eliminate varargs problems */
  925.  
  926. #define vout(s)  fprintf(vplout, s)
  927. int level ; /* the depth of parenthesis nesting in VPL file being written */
  928.  
  929. void vlevout() {
  930.    register int l = level ;
  931.    while (l--) vout("   ") ;
  932. }
  933.  
  934. void vlevnlout() {
  935.    vout("\n") ;
  936.    vlevout() ;
  937. }
  938.  
  939. #define voutln(str) {fprintf(vplout,"%s\n",str);vlevout();}
  940. /*
  941. void
  942. voutln(s)
  943. char *s ;
  944. {
  945.    register char l ;
  946.    fprintf(vplout,"%s\n", s) ;
  947.    for (l=level; l; l--) vout("   ") ;
  948. }
  949. */
  950. #define voutln2(f,s) {fprintf(vplout,f,s);vlevnlout();}
  951. /*
  952. void
  953. voutln2(f,s)
  954. char *f, *s ;
  955. {
  956.    register char l ;
  957.    (void) sprintf(buffer, f, s) ;
  958.    fprintf(vplout,"%s\n", buffer) ;
  959.    for (l=level; l; l--) vout("   ") ;
  960. }
  961. */
  962. #define voutln3(f,a,b) {fprintf(vplout,f,a,b);vlevnlout();}
  963. /*
  964. void
  965. voutln3(f,s1,s2)
  966. char *f, *s1, *s2 ;
  967. {
  968.    register char l ;
  969.    (void) sprintf(buffer, f, s1, s2) ;
  970.    fprintf(vplout,"%s\n", buffer) ;
  971.    for (l=level; l; l--) vout("   ") ;
  972. }
  973. */
  974. void
  975. vleft()
  976. {
  977.    level++ ;
  978.    vout("(") ;
  979. }
  980.  
  981. void
  982. vright()
  983. {
  984.    level-- ;
  985.    voutln(")") ;
  986. }
  987.  
  988. char vcharbuf[6] ;
  989. char *vchar(c)
  990. int c ;
  991. {
  992.    if ((c>='0' && c<='9')||(c>='A' && c<='Z')||(c>='a' && c<='z'))
  993.       (void) sprintf(vcharbuf,"C %c", c) ;
  994.    else (void) sprintf(vcharbuf,"O %o", c) ;
  995.    return (vcharbuf) ;
  996. }
  997.  
  998. void
  999. writevpl()
  1000. {
  1001.    register int i ;
  1002.    register struct adobeinfo *ai ;
  1003.    register struct lig *nlig ;
  1004.    register struct kern *nkern ;
  1005.    register struct pcc *npcc ;
  1006.    struct adobeinfo *asucc, *asub, *api ;
  1007.    int xoff, yoff, ht ;
  1008.    char unlabeled ;
  1009.  
  1010.    voutln2("(VTITLE Created by %s)", titlebuf) ;
  1011.    voutln("(COMMENT Please edit that VTITLE if you edit this file)") ;
  1012.    (void)sprintf(obuffer, "TeX-%s%s%s%s", outname,
  1013.       (efactor==1.0? "" : "-E"), (slant==0.0? "" : "-S"),
  1014.                  (makevpl==1? "" : "-CSC")) ;
  1015.    if (strlen(obuffer)>19) { /* too long, will retain first 9 and last 10 */
  1016.       register char *p, *q ;
  1017.       for (p = &obuffer[9], q = &obuffer[strlen(obuffer)-10] ; p<&obuffer[19];
  1018.               p++, q++) *p = *q ;
  1019.       obuffer[19] = '\0' ;
  1020.    }
  1021.    voutln2("(FAMILY %s)" , obuffer) ;
  1022.    voutln2("(CODINGSCHEME TeX text + %s)", codingscheme) ;
  1023.    voutln("(DESIGNSIZE R 10.0)") ;
  1024.    voutln("(DESIGNUNITS R 1000)") ;
  1025.    voutln("(COMMENT DESIGNSIZE (1 em) IS IN POINTS)") ;
  1026.    voutln("(COMMENT OTHER DIMENSIONS ARE MULTIPLES OF DESIGNSIZE/1000)") ;
  1027.    voutln2("(CHECKSUM O %lo)",cksum ^ 0xffffffff) ;
  1028.    vleft() ; voutln("FONTDIMEN") ;
  1029.    if (newslant)
  1030.       voutln2("(SLANT R %f)", newslant) ;
  1031.    voutln2("(SPACE D %d)", transform(fontspace,0)) ;
  1032.    if (! fixedpitch) {
  1033.       voutln2("(STRETCH D %d)", transform(200,0)) ;
  1034.       voutln2("(SHRINK D %d)", transform(100,0)) ;
  1035.    }
  1036.    voutln2("(XHEIGHT D %d)", xheight) ;
  1037.    voutln2("(QUAD D %d)", transform(1000,0)) ;
  1038.    voutln2("(EXTRASPACE D %d)", transform(fixedpitch ? fontspace : 111, 0)) ;
  1039.    vright() ;
  1040.    vleft() ; voutln("MAPFONT D 0");
  1041.    voutln2("(FONTNAME %s)", outname) ;
  1042.    voutln2("(FONTCHECKSUM O %lo)", cksum) ;
  1043.    vright() ;
  1044.    if (makevpl>1) {
  1045.       vleft() ; voutln("MAPFONT D 1");
  1046.       voutln2("(FONTNAME %s)", outname) ;
  1047.       voutln("(FONTAT D 800)") ;
  1048.       voutln2("(FONTCHECKSUM O %lo)", cksum) ;
  1049.       vright() ;
  1050.    }
  1051.  
  1052.    for (i=0; i<256 && texptrs[i]==NULL; i++) ;
  1053.    bc = i ;
  1054.    for (i=255; i>=0 && texptrs[i]==NULL; i--) ;
  1055.    ec = i;
  1056.  
  1057.    vleft() ; voutln("LIGTABLE") ;
  1058.    for (i=bc; i<=ec; i++)
  1059.       if (ai=texptrs[i]) {
  1060.          unlabeled = 1 ;
  1061.          if (uppercase[i]==NULL) /* omit ligatures from smallcap lowercase */
  1062.             for (nlig=ai->ligs; nlig; nlig=nlig->next)
  1063.                if (asucc=findadobe(nlig->succ))
  1064.                   if (asucc->texnum>=0)
  1065.                      if (asub=findadobe(nlig->sub))
  1066.                         if (asub->texnum>=0) {
  1067.                            if (unlabeled) {
  1068.                               voutln2("(LABEL %s)", vchar(i)) ;
  1069.                               unlabeled = 0 ;
  1070.                            }
  1071.                            voutln3("(LIG %s O %o)", vchar(asucc->texnum),
  1072.                                 asub->texnum) ;
  1073.                         }
  1074.          for (nkern = (uppercase[i] ? uppercase[i]->kerns : ai->kerns);
  1075.                     nkern; nkern=nkern->next)
  1076.             if (asucc=findadobe(nkern->succ))
  1077.                if (asucc->texnum>=0)
  1078.                   if (uppercase[asucc->texnum]==NULL) {
  1079.                      if (unlabeled) {
  1080.                         voutln2("(LABEL %s)", vchar(i)) ;
  1081.                         unlabeled = 0 ;
  1082.                      }
  1083.                      if (uppercase[i]) {
  1084.                         if (lowercase[asucc->texnum]) {
  1085.                            voutln3("(KRN %s R %.1f)",
  1086.                                  vchar(lowercase[asucc->texnum]->texnum),
  1087.                                  0.8*nkern->delta) ;
  1088.                         } else voutln3("(KRN %s R %.1f)",
  1089.                                  vchar(asucc->texnum), 0.8*nkern->delta) ;
  1090.                      } else {
  1091.                         voutln3("(KRN %s R %d)", vchar(asucc->texnum),
  1092.                                 nkern->delta) ;
  1093.                         if (lowercase[asucc->texnum])
  1094.                            if (lowercase[asucc->texnum]->texnum>=0)
  1095.                               voutln3("(KRN %s R %.1f)",
  1096.                                 vchar(lowercase[asucc->texnum]->texnum),
  1097.                                 0.8*nkern->delta) ;
  1098.                      }
  1099.                   }
  1100.          if (! unlabeled) voutln("(STOP)") ;
  1101.       }
  1102.    vright() ;
  1103.  
  1104.    for (i=bc; i<=ec; i++)
  1105.       if (ai=texptrs[i]) {
  1106.          vleft() ; fprintf(vplout, "CHARACTER %s", vchar(i)) ;
  1107.          if (*vcharbuf=='C') {
  1108.             voutln("") ;
  1109.          } else
  1110.             voutln2(" (comment %s)", ai->adobename) ;
  1111.          if (uppercase[i]) {
  1112.             ai=uppercase[i] ;
  1113.             voutln2("(CHARWD R %.1f)", 0.8 * (ai->width)) ;
  1114.             if (ht=texheight(ai)) voutln2("(CHARHT R %.1f)", 0.8 * ht) ;
  1115.             if (ai->lly) voutln2("(CHARDP R %.1f)", -0.8 * ai->lly) ;
  1116.             if (ai->urx > ai->width)
  1117.                voutln2("(CHARIC R %.1f)", 0.8 * (ai->urx - ai->width)) ;
  1118.          } else {
  1119.             voutln2("(CHARWD R %d)", ai->width) ;
  1120.             if (ht=texheight(ai)) voutln2("(CHARHT R %d)", ht) ;
  1121.             if (ai->lly) voutln2("(CHARDP R %d)", -ai->lly) ;
  1122.             if (ai->urx > ai->width)
  1123.                voutln2("(CHARIC R %d)", ai->urx - ai->width) ;
  1124.          }
  1125.          if (ai->adobenum != i || uppercase[i]) {
  1126.             vleft() ; voutln("MAP") ;
  1127.             if (uppercase[i]) voutln("(SELECTFONT D 1)") ;
  1128.             if (ai->pccs) {
  1129.                xoff = 0 ; yoff = 0 ;
  1130.                for (npcc = ai->pccs; npcc; npcc=npcc->next)
  1131.                   if (api=findadobe(npcc->partname))
  1132.                      if (api->texnum>=0) {
  1133.                         if (npcc->xoffset != xoff) {
  1134.                            if (uppercase[i]) {
  1135.                               voutln2("(MOVERIGHT R %.1f)",
  1136.                                       0.8 * (npcc->xoffset - xoff)) ;
  1137.                            } else voutln2("(MOVERIGHT R %d)",
  1138.                                       npcc->xoffset - xoff) ;
  1139.                            xoff = npcc->xoffset ;
  1140.                         }
  1141.                         if (npcc->yoffset != yoff) {
  1142.                            if (uppercase[i]) {
  1143.                               voutln2("(MOVEUP R %.1f)",
  1144.                                       0.8 * (npcc->yoffset - yoff)) ;
  1145.                            } else voutln2("(MOVEUP R %d)",
  1146.                                       npcc->yoffset - yoff) ;
  1147.                            yoff = npcc->yoffset ;
  1148.                         }
  1149.                         voutln2("(SETCHAR O %o)", api->adobenum) ;
  1150.                         xoff += texptrs[api->texnum]->width ;
  1151.                      }
  1152.             } else voutln2("(SETCHAR O %o)", ai->adobenum) ;
  1153.             vright() ;
  1154.          }
  1155.          vright() ;
  1156.       }
  1157.    if (level) error("! I forgot to match the parentheses") ;
  1158. }
  1159.  
  1160. void
  1161. openfiles(argc, argv)
  1162. int argc ;
  1163. char *argv[] ;
  1164. {
  1165.    register int lastext ;
  1166.    register int i ;
  1167.    extern void exit() ;
  1168.    tfmout = (FILE *)NULL ;
  1169.  
  1170.    if (argc == 1) {
  1171.       (void)printf("afm2tfm 5.36, Copyright 1990 by Radical Eye Software\n") ;
  1172.       (void)
  1173.         printf("Usage: afm2tfm foo[.afm] [-v|-V bar[.vpl]] [foo[.tfm]]\n") ;
  1174.      exit(0) ;
  1175.    }
  1176.  
  1177.    (void)sprintf(titlebuf, "%s %s", argv[0], argv[1]) ;
  1178.    (void)strcpy(inname, argv[1]) ;
  1179.    lastext = -1 ;
  1180.    for (i=0; inname[i]; i++)
  1181.       if (inname[i] == '.')
  1182.          lastext = i ;
  1183.       else if (inname[i] == '/' || inname[i] == ':')
  1184.          lastext = -1 ;
  1185.    if (lastext == -1) (void)strcat(inname, ".afm") ;
  1186.    if ((afmin=fopen(inname, "r"))==NULL)
  1187.       error("! can't open afm input file") ;
  1188.  
  1189.    while (argc>3 && *argv[2]=='-') {
  1190.       switch (argv[2][1]) {
  1191. case 'V': makevpl++ ;
  1192. case 'v': makevpl++ ;
  1193.          (void)strcpy(outname, argv[3]) ;
  1194.          lastext = -1 ;
  1195.          for (i=0; outname[i]; i++)
  1196.             if (outname[i] == '.')
  1197.                lastext = i ;
  1198.             else if (outname[i] == '/' || outname[i] == ':')
  1199.                lastext = -1 ;
  1200.          if (lastext == -1) (void)strcat(outname, ".vpl") ;
  1201.          if ((vplout=fopen(outname, WRITEBIN))==NULL)
  1202.             error("! can't open vpl output file") ;
  1203.          break ;
  1204. case 'e': if (sscanf(argv[3], "%f", &efactor)==0 || efactor<0.01)
  1205.             error("! Bad extension factor") ;
  1206.          break ;
  1207. case 's': if (sscanf(argv[3], "%f", &slant)==0)
  1208.             error("! Bad slant parameter") ;
  1209.          break ;
  1210. case 't':  /* get tfm name if present */
  1211.          (void)strcpy(outname, argv[3]) ;
  1212.          lastext = -1 ;
  1213.          for (i=0; outname[i]; i++)
  1214.             if (outname[i] == '.')
  1215.                lastext = i ;
  1216.             else if (outname[i] == '/' || outname[i] == ':')
  1217.                     lastext = -1 ;
  1218.         if (lastext == -1) {
  1219.            lastext = strlen(outname) ;
  1220.            (void)strcat(outname, ".tfm") ;
  1221.         }
  1222.         if ((tfmout=fopen(outname, WRITEBIN))==NULL)
  1223.            error("! can't open tfm output file") ;
  1224.         break ;
  1225. default: (void)fprintf(stderr, "Unknown option %s %s will be ignored.\n",
  1226.                          argv[2], argv[3]) ;
  1227.       }
  1228.       (void)sprintf(titlebuf, "%s %s %s", titlebuf, argv[2], argv[3]) ;
  1229.       argv += 2 ;
  1230.       argc -= 2 ;
  1231.    }
  1232.  
  1233.    if (argc>3 || (argc==3 && *argv[2]=='-'))
  1234.       error("! Usage: afm2tfm foo[.afm] [-v|-V bar[.vpl]] [foo[.tfm]]") ;
  1235.          
  1236.    if (argc == 2) (void)strcpy(outname, inname) ;
  1237.    else (void)strcpy(outname, argv[2]) ;
  1238.  
  1239.    lastext = -1 ;
  1240.    for (i=0; outname[i]; i++)
  1241.       if (outname[i] == '.')
  1242.          lastext = i ;
  1243.       else if (outname[i] == '/' || outname[i] == ':' || outname[i] == '\\')
  1244.          lastext = -1 ;
  1245.    if (argc == 2) {
  1246.       outname[lastext] = 0 ;
  1247.       lastext = -1 ;
  1248.    }
  1249.    if (lastext == -1) {
  1250.       lastext = strlen(outname) ;
  1251.       (void)strcat(outname, ".tfm") ;
  1252.    }
  1253.    if (tfmout == NULL && (tfmout=fopen(outname, WRITEBIN))==NULL)
  1254.       error("! can't open tfm output file") ;
  1255.    outname[lastext] = 0 ;
  1256. /*
  1257.  *   Now we strip off any directory information, so we only use the
  1258.  *   base name in the vf file.  We accept any of /, :, or \ as directory
  1259.  *   delimiters, so none of these are available for use inside the
  1260.  *   base name; this shouldn't be a problem.
  1261.  */
  1262.    for (i=0, lastext=0; outname[i]; i++)
  1263.       if (outname[i] == '/' || outname[i] == ':' || outname[i] == '\\')
  1264.          lastext = i + 1 ;
  1265.    if (lastext)
  1266.       strcpy(outname, outname + lastext) ;
  1267. }
  1268.  
  1269. #ifndef VMS
  1270. void
  1271. #endif
  1272. main(argc, argv)
  1273. int argc ;
  1274. char *argv[] ;
  1275. {
  1276.    extern void exit() ;
  1277.  
  1278.    openfiles(argc, argv) ;
  1279.    readadobe() ;
  1280.    buildtfm() ;
  1281.    writetfm() ;
  1282.    if (makevpl) {
  1283.       assignchars() ;
  1284.       if (makevpl>1) upmap() ;
  1285.       writevpl() ;
  1286.    }
  1287.    exit(0) ;
  1288.    /*NOTREACHED*/
  1289. }
  1290.